package com.example.yeujian5.yuejian5.model;


import com.example.yeujian5.yuejian5.Interface.PictureFace;
import com.example.yeujian5.yuejian5.Listenner.PictureCommentListener;
import com.example.yeujian5.yuejian5.Listenner.PictureInfoListener;
import com.example.yeujian5.yuejian5.Listenner.PictureListener;
import com.example.yeujian5.yuejian5.Listenner.PostListener;
import com.example.yeujian5.yuejian5.bean.PictureBean;



import com.example.yeujian5.yuejian5.service.PictureService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 7h0rns on 2017/10/11.
 */

public  class PictureModel implements PictureFace {
    private Retrofit retrofit;
    private PictureService pictureService;
    private static final String BASE_URL = "http://172.24.10.175/workout/api.php/";//主机地址

    public PictureModel() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 获得列表

     * @param listListener 监听对象
     */
    @Override
    public void getPhotoList(String id, String title,final PictureListener listListener) {
        pictureService = retrofit.create(PictureService.class);
        Call<List<PictureBean>> call =pictureService.photolist();
        call.enqueue(new Callback<List<PictureBean>>() {
            @Override
            public void onResponse(Call<List<PictureBean>> call, Response<List<PictureBean>> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<List<PictureBean>> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }
    @Override
    public void getPhotoByID(String id,final PictureInfoListener listListener) {
        pictureService = retrofit.create(PictureService.class);
        Call<PictureBean> call =pictureService.getPhotoById(id);
        call.enqueue(new Callback<PictureBean>() {
            @Override
            public void onResponse(Call<PictureBean> call, Response<PictureBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<PictureBean> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }


    @Override
    public void insertComment(String shaitu_id, String userid, String reply_userid, String reply, final PictureCommentListener listListener) {
        pictureService = retrofit.create(PictureService.class);
        Call<Integer> call =pictureService.insertComment(shaitu_id,userid,reply_userid,reply);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                if (response.body() > 0) {
                    listListener.onResponse(response.body());
                } else {
                    listListener.onFail("fail");
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                listListener.onFail(t.toString());
            }
        });
    }

}
